home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9373 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Input into array of strings?
  5. Date: 9 Mar 1996 18:12:17 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4ht38h$d1@umbc9.umbc.edu>
  8. References: <4hnv1e$o7n@rhea.glo.be>
  9. NNTP-Posting-Host: umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Sven Claeys <sclaeys@glo.be> wrote:
  13. |> I've got a some kind of problem reading into arrays of strings. I'm a
  14. |> bit rusty in C or C++, it's been a while, so please be patient with
  15. |> me.
  16. |> 
  17. |> here we go:
  18. |> 
  19. |> i've got a structure
  20. |> 
  21. |> struct ImmoData
  22. |> {
  23. |>    char code[3][5];
  24.  
  25. Note that this can be treated as either a 3x5 2D array of char or an
  26. array of 3 strings of length 4 which are NUL terminated.
  27. |>    ....
  28. |> }....;
  29. |> 
  30. |> now i want to read data from a file with fgets(...) into this array.
  31. |> 
  32. |> I've tried
  33. |>    for (i=0; i<5; i++)
  34. |>        fgets(code[i],....);
  35.  
  36. Well there's the problem. You only have 3 array elements so 5 is incorrect.
  37. Make it:
  38.  
  39. for (i = 0; i < 3; i++)
  40.    fgets (code[i], 5, myinputstream);
  41.  
  42. |> or
  43. |>    for (i=0; i<5; i++)
  44. |>        fgets((char*) code[i], ...);
  45.  
  46. Same as above except the cast is unnecessary.
  47.  
  48. |> but none of these seem to work. As i'm debugging the first code is
  49. |> read correct, but the second is appended to the first and is also read
  50. |> into the second, and so on.
  51. |> 
  52. |> Could anyone help me with this strange phenomenon?
  53.  
  54. A phenomenon? Nothing so fancy...Just accessing memory which is not
  55. properly designated for your use which produces undefined results.
  56. -- 
  57. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  58.  
  59. Jonas J. Schlein  (schlein@gl.umbc.edu)
  60.